using UnityEngine;

public class IS_Tank_Script : MonoBehaviour
{
    [Header("炮弹预设体")]
    public GameObject bulletPrefab;
    [Header("移动速度")]
    public float moveSpeed = 3f;
    [Header("转身速度")]
    public float turnSpeed = 1f;

    private Transform firePoint;
    private Rigidbody rigidbody;
    private float hor,ver;
    private bool fire;
    private int score = 0;

    private void Awake()
    {
        // 获取炮弹生成位置
        firePoint = transform.Find("Top/Gun/FirePoint");
        // 获取刚体组件
        rigidbody = GetComponent<Rigidbody>();
    }

    void Update()
    {
        hor = Input.GetAxis("Horizontal");
        ver = Input.GetAxis("Vertical"); 
        fire = Input.GetButtonDown("Fire1");
        // 前后移动
        transform.position += transform.forward * ver * Time.deltaTime * moveSpeed;
        // 左右转向
        transform.eulerAngles += Vector3.up * hor * turnSpeed;
        // 发射炮弹
        if (fire)
        {
            // 生成炮弹
            GameObject bullet = Instantiate(bulletPrefab, firePoint.position, Quaternion.identity);
            // 给炮弹刚体rigid赋予速度
            bullet.GetComponent<IS_Bullet_Script>().direction = transform.forward;
            // 3秒后，销毁炮弹
            // Destroy(bullet, 3);
        }
        // 坦克弹跳-给刚体添加速度
        if (Input.GetKeyDown(KeyCode.Space))
        {
            rigidbody.velocity = Vector3.up*5;
        }
        // 推坦克-给刚体添加力
        if (Input.GetKey(KeyCode.N))
        {
            rigidbody.AddForce(Vector3.forward*10);
        }
        // 炸飞坦克-给刚体添加爆炸力
        if (Input.GetKeyDown(KeyCode.B))
        {
            rigidbody.AddExplosionForce(1000, transform.position+Vector3.right, 5);
        }
    }
    
    /* ========================================== 触发器 ============================================== */
    /* 触发器的触发条件:
            1.碰撞双方都包含Collider碰撞器组件
            2.需要其中一方包含RigidBody刚体组件
            3.需要其中一方的Collider碰撞器的属性设置为trigger触发器
     */
    
    /* 触发开始时调用一次 */
    private void OnTriggerEnter(Collider other)
    {
        // 如果接到了金币，分数加1，销毁金币
        if (other.tag == "Coin")
        {
            Debug.Log("score: " + ++score);
            Destroy(other.gameObject);
        }
    }
  
    /* 触发过程中持续调用，每帧一次 */
    private void OnTriggerStay(Collider other)
    {
        
    }

    /* 触发结束时调用一次 */
    private void OnTriggerExit(Collider other)
    {
        
    }
}